
package kaizone.haining.chaobohui.widget;

import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.json.Item;
import kaizone.haining.chaobohui.json.Item.PicModel;
import kaizone.haining.chaobohui.json.JSONResponse;
import kaizone.haining.chaobohui.login.UserControl;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.util.Utils;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DetailItemCard extends LinearLayout implements OnClickListener,
        DataControl.IDataUpdateListener {

    private TextView mTitleTextView;

    private TextView mTimeTextView;

    private TextView mAddressTextView;

    private TextView mContactNameTextView;

    private TextView mContactNumTextView;

    private ImageView mImageView;

    private ImageView mButton;

    private ImageFetcher mImageFetcher;

    private OnClickListener mClickListener;

    private UserControl mUserControl;

    private DataControl mDataControl;

    private Object mTempData;

    private String mRequestActivityString;

    public DetailItemCard(Context context) {
        super(context);
        init();
    }

    public DetailItemCard(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mUserControl = new UserControl(getContext());
        mDataControl = new DataControl(getContext());
        mDataControl.addChangeListener(this);
    }

    private void signup() {
        final SignupAlert signupalert = new SignupAlert(getContext());
        signupalert.setTitle("活动报名");
        signupalert.setPositiveButton("确定", new AlertDialog.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String phone = signupalert.getInputText02();
                String name = signupalert.getInputText01();
                requestActivitySignup(phone, name);
            }
        });
        signupalert.setNegativeButton("取消", null);
        signupalert.show();
    }

    private void requestActivitySignup(String contactPhone, String contactName) {
        if (mTempData == null)
            return;
        if (!(mTempData instanceof Item))
            return;
        final int activityId = ((Item) mTempData).getId();
        mRequestActivityString = mUserControl.requestActivitySignup(activityId, contactPhone,
                contactName);
        mDataControl.exec(mRequestActivityString);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTitleTextView = (TextView) findViewById(R.id.tv01);
        mTimeTextView = (TextView) findViewById(R.id.tv02);
        mAddressTextView = (TextView) findViewById(R.id.tv03);
        mContactNameTextView = (TextView) findViewById(R.id.tv04);
        mContactNumTextView = (TextView) findViewById(R.id.tv05);
        mImageView = (ImageView) findViewById(R.id.img);
        mButton = (ImageView) findViewById(R.id.btn);
        mButton.setOnClickListener(this);
        // mButton.setVisibility(View.GONE);
    }

    public void setData(Item item) {
        if (item != null) {
            mTempData = item;
            mTitleTextView.setText(item.getName());
            String gmtactstart = Utils.subString(item.getGmtActivityStart(), "T");
            String gmtactend = Utils.subString(item.getGmtActivityStart(), "T");
            StringBuilder time = new StringBuilder();
            time.append(gmtactstart).append("~").append(gmtactend);
            mTimeTextView.append(time.toString());
            mAddressTextView.append(item.getLocation());
            mContactNameTextView.append(item.getContactName());
            mContactNumTextView.append(item.getContactPhone());
            List<PicModel> picmodels = item.getPicModelList();
            if (picmodels != null && !picmodels.isEmpty()) {
                String x100 = picmodels.get(0).sum100x000;
                mImageFetcher.loadImage(x100, mImageView);
            }
            String cansignup = item.getCanSignup();
            if (!TextUtils.isEmpty(cansignup)) {
                if (cansignup.equals(Item.CANSIGNUP_F)) {
                    mButton.setVisibility(View.GONE);
                }//
                else if (cansignup.equals(Item.CANSIGNUP_T)) {
                    mButton.setVisibility(View.VISIBLE);
                }
            } else {
                mButton.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public Object backParse(String datastring, String requeststring) {
        if (TextUtils.isEmpty(datastring))
            return null;
        if (requeststring.equals(mRequestActivityString)) {
            JSONResponse response = JSONResponse.parseJsonString(datastring);
            return response;
        }
        return null;
    }

    @Override
    public void foreUpdate(Object result, String requeststring) {
        if (result == null)
            return;
        if (requeststring.equals(mRequestActivityString)) {
            if (result instanceof JSONResponse) {
                JSONResponse response = (JSONResponse) result;
                if (response.success) {
                    Toast.makeText(getContext(), "报名成功", 800).show();
                } else {
                    Toast.makeText(getContext(), "报名失败", 800).show();
                }
            }

        }//
    }

    @Override
    public void exception(Exception e) {
        // TODO Auto-generated method stub

    }

    public ImageFetcher getImageFetcher() {
        return mImageFetcher;
    }

    public void setImageFetcher(ImageFetcher imageFetcher) {
        this.mImageFetcher = imageFetcher;
    }

    @Override
    public void onClick(View v) {
        if (mClickListener != null && v == mButton) {
            mClickListener.onButtonClick(v);
        }
        signup();
    }

    public void setOnClickListener(DetailItemCard.OnClickListener listener) {
        mClickListener = listener;
    }

    public interface OnClickListener {
        void onButtonClick(View v);
    }

    public static class SignupAlert extends AlertDialog.Builder {

        private EditText mEditText01;

        private EditText mEditText02;

        public SignupAlert(Context context) {
            super(context);
            View view = View.inflate(context, R.layout.users_signup, null);
            // setContentView(R.layout.users_signup);
            mEditText01 = (EditText) view.findViewById(R.id.edittext01);
            mEditText02 = (EditText) view.findViewById(R.id.edittext02);
            setView(view);
        }

        public String getInputText01() {
            return mEditText01.getText().toString();
        }

        public String getInputText02() {
            return mEditText02.getText().toString();
        }
    }

}
